<template>
    <nav class="sidebar">
        <div>
            <img src="../../src/images/logo.png" alt="">
        </div>
        <ul class="left_nav">
            <li 
                v-for="(item,index) in navList" 
                :key="index" 
                >
                <router-link 
                  tag="a" 
                  :to="item.path" 
                  :class="{ active: isActive(item.path, 'top') }"
                >
                  {{item.title}}
                </router-link>
            </li>
        </ul>

        <ul class="nav_bottom">
          <li 
                v-for="(item,index) in navBottomList" 
                :key="index" 
                :class="{ active: isActive(item.path, 'bottom') }"
                >
                <router-link 
                  tag="a" 
                  :to="item.path" 
                  
                >
                  {{item.title}}
                </router-link>
            </li>
            <!-- <li><a href="#">联系我们</a></li>
            <li><a href="#">消息中心</a></li>
            <li><a href="#">使用指南</a></li>
            <li><a href="#">帮助与反馈</a></li> -->
        </ul>
    </nav>
    <PopupModal></PopupModal>
</template>
<script setup>
    import { useRoute } from 'vue-router'; // 导入 useRoute
    import { reactive } from 'vue';
    import { useUserStore } from '../store/user';
    const { userState, setToken } = useUserStore();
    const route = useRoute(); // 获取当前路由信息

    if ( !userState.token ) {
      if ( route.name.includes('AIMusic') ) {
        userState.isShowLogin = userState.betaVersion? true: false
      } else {
        setTimeout(() => {
          userState.isShowLogin = true
        },1000)
      }
      console.log('未登录用户需登录')
      userState.pageIndex = 0
      userState.logintabNum = 0
    }

    
    const navList = reactive([
        { title: '首页', path: '/' },
        { title: 'AI作曲', path: '/aimusic' },
        { title: '我的创作', path: '/myworks' },
        { title: 'AI模型库', path: '/aimodel' },
        { title: '商城', path: '/shop' }
    ])

    const navBottomList = reactive([
        { title: '联系我们', path: '/contactUs' },
        { title: '消息中心', path: '/messageCenter' },
        { title: '使用指南', path: '/useDuide' },
        { title: '帮助与反馈', path: '/help' },
    ])

    // 判断当前路径是否与给定路径匹配
    function isActive(path, type) {
      if (type === 'top') {
        // 对于顶部导航，进行精确匹配
        return route.path === path;
      }

      if (type === 'bottom') {
        // 对于底部导航，使用 startsWith() 来进行路径前缀匹配
        return route.path.startsWith(path);
      }

      return false;
    }
</script>

<style scoped>
.sidebar {
    width: 200px; 
    background: linear-gradient(180deg, rgba(42, 42, 65, 1) 0%, rgba(71, 73, 83, 1) 100%);
    color: white; 
    padding: 10px;
    box-sizing: border-box;
    height: 100vh; /* 最小高度为视口高度，确保撑满 */
    min-height: 683px !important;
    position: relative;
    font-size: 22px;
}

.left_nav {
      width: 100%;
      margin-top: 30px;
      /* background-color: brown; */
      padding-bottom: 40px;
      border-bottom: 1px solid #5a5c6c;
  }
  
  .left_nav li {
      width: 100%;
      font-size: 22px;
      padding: 5px 0;
      box-sizing: border-box;
      margin-bottom: 1px;
  }
  
  .left_nav li a {
      height: 40px;
      line-height: 40px;
      padding:0px 20px;
      color: #fff;
      display: block;
      position: relative;
      border-radius: 10px;
      display: block;
      transition: all .5s;
  }

  .left_nav li a:hover{
    background-color: #636672;
  }
  
  .left_nav li .active {
      background-color: #636672;
  }

  .nav_bottom {
    width: 100%;
    position: absolute;
    bottom: 0;
    left: 0;
    padding: 20px 0;
    box-sizing: border-box;
    border-top: 1px solid #5a5c6c;

  }

  .nav_bottom li {
    width: 100%;
    height: 40px;
    font-size: 20px;
    padding: 5px 0;
    box-sizing: border-box;
    margin-bottom: 1px;
    position: relative;
  }

  .nav_bottom li::before{
    content:'';
    width: 100%;
    height: 100%;
    background: linear-gradient(106deg, rgba(164,141,222,1) 38.72%, rgba(89,76,120, 1) 91.25%);
    /* background-color: red; */
    position: absolute;
    left: 0;
    top: 0;
    bottom: 0;
    right: 0;
    display: block;
    z-index: 1;

    opacity: 0;
    transition: all .5s;
  }

  .nav_bottom li.active::before,
  .nav_bottom li:hover::before {
    opacity: 1;
    /* background: linear-gradient(106deg, rgba(164,141,222,1) 38.72%, rgba(89,76,120, 1) 91.25%); */
  }

  .nav_bottom a {
    width: 100%;
    height: 40px;
    line-height: 40px;
    padding-left: 50px;
    color: #fff;
    position: absolute;
    left: 0;
    top: 0;
    right: 0;
    bottom: 0;
    border-radius: 10px;
    display: block;
    transition: all .5s;
    background-color: red;
    z-index: 2;
  }

  .nav_bottom li:nth-child(1) a{
    background: url(../images/icon_contact.png) no-repeat 20px center;
  }
  .nav_bottom li:nth-child(2) a{
    background: url(../images/icon_message.png) no-repeat 20px center;
  }
  .nav_bottom li:nth-child(3) a{
    background: url(../images/icon_guide.png) no-repeat 20px center;
  }
  .nav_bottom li:nth-child(4) a{
    background: url(../images/icon_help_small.png) no-repeat 20px center;
  }
  .nav_bottom li:nth-child(1) a,
  .nav_bottom li:nth-child(2) a,
  .nav_bottom li:nth-child(3) a,
  .nav_bottom li:nth-child(4) a{
    background-size: 8%;
  }
  
</style>